#!/bin/bash

current_path=`pwd`
param=$1

isValidPath() {
  # valid directory or file path
  if [ -d "$current_path" -o -f "$current_path" ]; then
    if [ -f "$current_path" ]; then
        # if it is a file, assign the current_path to the file's parent directory
        current_path=$(dirname "$current_path")
    fi
    return 1
  fi
  return 0
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
  echo "Usage: openDir [path] \nNotice: default path is current path"
else
  if [ -n ${param} ] && [ ${param} ]; then
    current_path=${param}
  fi
  isValidPath
  result=$?
  if [ ${result} = "1" ]; then
    nautilus ${current_path}
  else
    echo "Error: invalid path: ${current_path}"
  fi
fi